package com.wuax.controller;

import com.wuax.domain.LoginUser;
import com.wuax.domain.SysUser;
import com.wuax.domain.vo.Result;
import com.wuax.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class UserController {

    @Autowired
    private SysUserService sysUserService;

    /**
     * 登录页面
     */
    @GetMapping("/login")
    private String login() {
        return "login";
    }

    /**
     * 登录验证
     */
    @PostMapping("/loginCheck")
    @ResponseBody
    public Result loginCheck(LoginUser loginUser, HttpSession session) {
        return sysUserService.login(loginUser, session);
    }

    /**
     * 登出
     */
    @GetMapping("/logout")
    public String logout(HttpSession session) {
        // 获取当前登录用户
        SysUser user = (SysUser) session.getAttribute("sys_user");
        // 销毁掉当前session
        session.invalidate();
        if (user == null) {
            // 如果当前用户session被销毁则登出
            return "redirect:login";
        } else {
            return "redirect:/";
        }
    }
}
